1312E - Array Shrinking - CodeForces Solution


dp greedy *2100

Please click on ads to support us..

C++ Code:

// Problem: E. Array Shrinking
// Contest: Codeforces - Educational Codeforces Round 83 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1312/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define ll long long  
const int N =500+10;
int dp[N][N],v[N][N];
signed main (){
	std::ios::sync_with_stdio(false);  
	cin.tie(NULL); 
	cout.tie(NULL);
	int n;
	cin>>n;
	
	for(int i=1;i<=n;i++)
	{
		cin>>v[i][i];
	}
	for(int i=2;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			int r=j+i-1;
			if(r>n)
			break;
			
			
			for(int k=j;k<r;k++)
			{	//if(i==1)
			//cout<<i<<endl;
				dp[j][r]=max(dp[j][r],dp[j][k]+dp[k+1][r]);
				if(v[j][k]==v[k+1][r]&&v[j][k]!=0)
				{
					dp[j][r]=i-1;
					v[j][r]=v[j][k]+1;
					
				}
			}
		}
	cout<<n-dp[1][n]<<endl;
	
} 


Comments

Submit
0 Comments
More Questions

1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year